package com.medical.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.config.SystemContext;
import com.medical.dao.SysNhjgMapper;
import com.medical.dao.SysUserMapper;
import com.medical.entity.SysNhjg;
import com.medical.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;


@Service
@Transactional
public class NhjgService {

    @Autowired
    private SysNhjgMapper sysNhjgMapper;


    /**
     * 根据用户ID查找用户信息
     * @param nhjgId
     * @return
     */
    public SysNhjg findNhjgByNhjgId(Integer nhjgId) {
        return sysNhjgMapper.findNhjgByNhjgId(nhjgId);
    }

    /**
     * 查找用户列表
     * @return
     */
    public PageInfo<SysNhjg> getNhjgList(String nhjgName, Integer currentPage, Integer limit){
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysNhjg> nhjgList = sysNhjgMapper.getNhjgList(nhjgName);
        PageInfo<SysNhjg> pageInfo = new PageInfo<SysNhjg>(nhjgList);
        return pageInfo;
    }

    /**
     * 根据ID获取用户信息
     * @param id
     * @return
     */
    public SysNhjg getNhjgById(Integer id) {
        return sysNhjgMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param sysNhjg
     */
    public void addNhjg(SysNhjg sysNhjg) {
        sysNhjg.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysNhjg.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        sysNhjg.setnhjgOther(SystemContext.passwdEncryption(sysNhjg.getnhjgOther()));

        sysNhjgMapper.insert(sysNhjg);
    }

    /**
     * 更新
     * @param sysNhjg
     */
    public void edit(SysNhjg sysNhjg) {
        sysNhjg.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysNhjgMapper.updateByPrimaryKeySelective(sysNhjg);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id) {
        sysNhjgMapper.deleteByPrimaryKey(id);
    }

}
